import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../res/resources.dart';

class MyAppBar extends StatefulWidget implements PreferredSizeWidget {
  final Color backgroundColor;
  final String title;
  final String centerTitle;
  final String actionName;
  final String backImg;
  final VoidCallback onPressed;
  final bool isBack;
  final double elevation;

  const MyAppBar(
      {Key key,
      this.backgroundColor: Colors.white, //背景色
      this.title: '',
      this.centerTitle: '',
      this.actionName: '',
      this.backImg: 'assets/images/ic_back_black.png',
      this.onPressed,
      this.elevation: 0,
      this.isBack: true})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return MyAppBarState();
  }

  @override
  Size get preferredSize => Size.fromHeight(45.0);
}

class MyAppBarState extends State<MyAppBar> {
  SystemUiOverlayStyle _overlayStyle = SystemUiOverlayStyle.light;

  Color getColor() {
    return _overlayStyle == SystemUiOverlayStyle.light
        ? Colors.white
        : Colorful.text_dark;
  }

  @override
  void initState() {
    super.initState();
    setState(() {
      _overlayStyle =
          ThemeData.estimateBrightnessForColor(widget.backgroundColor) ==
                  Brightness.dark
              ? SystemUiOverlayStyle.light
              : SystemUiOverlayStyle.dark;
    });
  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: _overlayStyle,
      child: Material(
        elevation: widget.elevation,
        color: widget.backgroundColor,
        child: SafeArea(
            child: Stack(
          alignment: Alignment.centerLeft,
          children: <Widget>[
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Container(
                  alignment: widget.centerTitle.isEmpty
                      ? Alignment.centerLeft
                      : Alignment.center,
                  width: double.infinity,
                  child: Text(
                    widget.title.isEmpty ? widget.centerTitle : widget.title,
                    style: TextStyle(
                      fontSize: Dimens.font_sp_16,
                      color: getColor(),
                    ),
                  ),
                  padding: const EdgeInsets.symmetric(horizontal: 45),
                ),
              ],
            ),
            widget.isBack
                ? IconButton(
                    onPressed: () {
                      Navigator.maybePop(context);
                    },
                    padding: const EdgeInsets.all(12),
                    icon: Image.asset(
                      widget.backImg,
                      color: getColor(),
                    ),
                  )
                : Gaps.empty,
            Positioned(
              right: 0,
              child: Theme(
                  data: ThemeData(
                    buttonTheme: ButtonThemeData(
                      padding: const EdgeInsets.symmetric(horizontal: 16),
                      minWidth: 55,
                    ),
                  ),
                  child: widget.actionName.isEmpty
                      ? Container()
                      : FlatButton(
                          child: Text(widget.actionName),
                          textColor: getColor(),
                          highlightColor: Colors.transparent,
                          onPressed: widget.onPressed,
                        )),
            ),
          ],
        )),
      ),
    );
  }
}
